﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class AddProduct : System.Web.UI.Page
{
	private SqlConnection _connection = new SqlConnection("server=127.0.0.1;uid=sa;pwd=;database=NorthwindCHS");

	protected void Page_Load(object sender, EventArgs e)
	{
		if (!Page.IsPostBack)
		{
			BindProductCategory();
		}
	}

	private void BindProductCategory()
	{
		DataSet ds = new DataSet();
		SqlDataAdapter da = new SqlDataAdapter(@"SELECT 类别ID, 类别名称 FROM 类别", _connection);
		da.Fill(ds, "Cateogry");
		dplCategory.DataSource = ds;
		dplCategory.DataTextField = "类别名称";
		dplCategory.DataValueField = "类别ID";
		dplCategory.DataBind();
	}

	private void AddProductFunction()
	{
		string categoryId = dplCategory.SelectedValue;
		string price = txtPrice.Text.Trim();
		string productName = txtProductName.Text.Trim();
		string quantity = txtQuantity.Text.Trim();

		SqlCommand command = new SqlCommand("INSERT INTO 产品(产品名称,类别ID,单价,库存量) VALUES('" + productName
											+ "'," + categoryId + "," + price + "," + quantity + ")", _connection);

		try
		{
			if (_connection.State != ConnectionState.Open)
			{
				_connection.Open();
			}

			command.ExecuteNonQuery();
		}
		catch (Exception exception)
		{
			throw new Exception(exception.Message);
		}
	}
	protected void btnAdd_Click(object sender, EventArgs e)
	{
		AddProductFunction();
		Response.Write("产品添加完毕，<a href=Default.aspx>点此返回</a>");
	}
}
